﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Objects;
using System.Linq;

namespace CDOP.Model
{
    [ScaffoldTable(false)]
    public partial class MasterTable
    {
        private static readonly CDOPEntities Db = new CDOPEntities();

        public bool IsCdopReferenceAvailable(string key)
        {
            var count = Db.MasterTable.Where(x => x.CDOP_Ref_Manual == key).Count();
            return count == 0;
        }

        public bool SaveMasterRecord(MasterTable recordToSave)
        {
            var isSuccess = false;
            try
            {
                if (recordToSave.CDOP_Ref == 0)
                    Db.AddToMasterTable(recordToSave);
                Db.SaveChanges();
                if (recordToSave.CDOP_Ref != 0)
                    isSuccess = true;

                return isSuccess;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool DeleteMasterRecord(int cdopRef)
        {
            try
            {
                var recordToDelete = Db.MasterTable.Where(x => x.CDOP_Ref == cdopRef).FirstOrDefault();
                Db.DeleteObject(recordToDelete);
                Db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public List<MasterTable> SearchByFirstName(int? startRowIndex, int? maximumRows, out int totalRecordCount, string firstName)
        {
            var outputParameter = new ObjectParameter("totalRows", typeof(int));
            var patients = Db.GetOrderedRecordsByFirstName(startRowIndex, maximumRows, firstName, outputParameter).ToList();
            totalRecordCount = (int)outputParameter.Value;
            return patients;
        }

        public List<MasterTable> SearchByLastName(int? startRowIndex, int? maximumRows, out int totalRecordCount, string lastName)
        {
            var outputParameter = new ObjectParameter("totalRows", typeof(int));
            var patients = Db.GetOrderedRecordsBySurName(startRowIndex, maximumRows, lastName, outputParameter).ToList();
            totalRecordCount = (int)outputParameter.Value;
            return patients;
        }

        public List<MasterTable> SearchByCdopRef(int? startRowIndex, int? maximumRows, out int totalRecordCount, string cdopRef)
        {
            var outputParameter = new ObjectParameter("totalRows", typeof(int));
            var patients = Db.GetOrderedRecordsByCDOP(startRowIndex, maximumRows, cdopRef, outputParameter).ToList();
            totalRecordCount = (int)outputParameter.Value;
            return patients;
        }

        public List<MasterTable> SearchByNhsRef(int? startRowIndex, int? maximumRows, out int totalRecordCount, string nhsNumber)
        {
            var outputParameter = new ObjectParameter("totalRows", typeof(int));
            var patients = Db.GetOrderedRecordsByNHS(startRowIndex, maximumRows, nhsNumber, outputParameter).ToList();
            totalRecordCount = (int)outputParameter.Value;
            return patients;
        }

        public List<MasterTable> GetAllPatients(int? startRowIndex, int? maximumRows, out int totalRecordCount)
        {
            var outputParameter = new ObjectParameter("totalRows", typeof(int));
            List<MasterTable> patients = Db.GetOrderedRecords(startRowIndex, maximumRows, outputParameter).ToList();
            totalRecordCount = (int)outputParameter.Value;
            return patients;
        }

        public MasterTable GetpatientByCdopRef(int cdopRef)
        {
            return Db.MasterTable.Where(x => x.CDOP_Ref == cdopRef).FirstOrDefault();
        }
    }
}